package com.tanhua.dubbo.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.SettingsMapper;
import com.tanhua.common.pojo.Settings;
import com.tanhua.common.pojo.User;
import com.tanhua.common.utils.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created with IntelliJ IDEA.
 *
 * @ClassName: SettingsService
 * @Author:王晨晗
 * @Description: 查询用户设置
 * @Date: 2022/01/08/22:36
 */
@Service
public class SettingsService {
    @Autowired
    private SettingsMapper settingsMapper;

    /**
     * @description 根据用户id查询用户的设置
     * @param userId
     * @return
     */
    public Settings querySettings(Long userId){
        //创建
        QueryWrapper<Settings> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id" , userId);
        return settingsMapper.selectOne(wrapper);
    }
    
    /**
     * @description 修改用户通知设置 
     * @param settings
     * @return 
     */
    public void updateSettings(Settings settings){
        //查询用户的设置信息
        Settings querySettings = querySettings(settings.getUserId());
        //判断是否为空
        if (ObjectUtil.isEmpty(querySettings)){
            //为空执行新增操作
            settingsMapper.insert(settings);
        }
        //修改通知参数值
        querySettings.setLikeNotification(settings.getLikeNotification());
        querySettings.setPinglunNotification(settings.getPinglunNotification());
        querySettings.setGonggaoNotification(settings.getGonggaoNotification());
        //执行修改
        settingsMapper.updateById(querySettings);
    }
}
